<template>
	<view>
		<view class="hiddenTitle">
		</view>
		<view class="cu-bar bg-white">
						<view class="action" @click="back">
							<text class="cuIcon-back text-gray"></text> 返回
						</view>
						<view class="content text-bold">
							权限修改
						</view>
					</view>
		<nickro-sortPickerList ref="sortPickerList" @clickData="clickData" data-target="RadioModal"></nickro-sortPickerList>
		
		<view class="cu-modal" :class="modalName=='RadioModal'?'show':''" @tap="hideModal">
			<view class="cu-dialog" @tap.stop="">
				<radio-group class="block" @change="RadioChange">
					<view class="cu-list menu text-left">
						<view class="cu-item" v-for="(item,index) in authorityList" :key="item.name">
							<label class="flex justify-between align-center flex-sub">
								<view class="flex-sub">{{item.name}}</view>
								<radio class="round" :class="radio==item.value?'checked':''" :checked="radio==item.value?true:false"
								 :value="item.value"></radio>
							</label>
						</view>
					</view>
				</radio-group>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				serverUrl: this.$globalUrl,
				modalName: null,
				attributeName: 'realName',
				authorityList: [
					{
						name: '普通用户',
						value: 'ROLE_USER'
					},
					{
						name: '管理员',
						value: 'ROLE_ADMIN'
					},
					{
						name: '超级管理员',
						value: 'ROLE_SADMIN'
					},
				],
				selectName: '',
				radio: '',
				dataArr: []
			}
		},
		onReady() {
			var that = this
			that.$refs["sortPickerList"].initPage(that.dataArr, that.attributeName)
		},
		onShow() {
			this.getUserList()
		},
		methods: {
			clickData(data) {
				console.log('姓名：' + data.realName)
				console.log('身份：' + data.authority)
				this.modalName = 'RadioModal'
				this.selectName = data.realName
				this.radio = data.authority
			},
			hideModal(e) {
				this.modalName = null
			},
			RadioChange(e) {
				this.radio = e.detail.value
				console.log('修改成：'+this.radio,'修改的人：'+this.selectName)
				this.updateAuthority()
				for(let i = 0;i < this.dataArr.length;i++){
					if(this.dataArr[i].realName == this.selectName){
						this.dataArr[i].authority = e.detail.value
					}
				}
				console.log(this.dataArr)
			},
			getUserList(){
				let self = this;
				uni.request({
						url: self.serverUrl+'/authority/list',
						header: {
							"Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
						},
						method: "POST"
					})
				.then(res => {
					console.log('接口请求成功',res)
					self.dataArr = res[1].data
				})
				.catch(error =>{
					console.log('接口请求失败',error)
				})
			},
			updateAuthority(){
				let self = this;
				uni.request({
						url: self.serverUrl+'/authority/updateAuthority',
						header: {
							"Authorization": uni.getStorageSync('tokenHead') + ' ' + uni.getStorageSync('token')
						},
						data: {
							authority: self.radio,
							realName: self.selectName
						},
						method: "POST"
					})
				.then(res => {
					wx.showToast({
					  title: '修改成功',
					  icon: 'success',
					  duration: 600
					})
				})
				.catch(error => {
					console.log(error)
				})
			},
			back(){
				uni.navigateBack({
				    delta: 1
				});
			}
		}
	}
</script>

<style>
.hiddenTitle{
		height: 45px;
		background-color: #FFFFFF;
	}
</style>
